<?php
/**
 * Created by WarCraft3
 * User: WarCraft3
 * Date: 2021/8/9
 * Time: 12:08
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\common\controller\Redis;
use tests\thinkphp\library\think\cache\driver\memcachedTest;
use think\Db;

class Auser extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    private $_uid;
    protected function _initialize()
    {
        parent::_initialize();
        $jwt = $this->request->header('Authorization');
        if($jwt){
            $this->check_token($jwt);
            $this->_uid = $this->_token['uid'];

        }else{
            $this->success('缺少token','','401');
        }
    }

    /*
     * 绑定个人信息
     * */
    public function bind()
    {
        $arr = $this->request->param();
        if ( !$this->request->param('cate_id')){
            $this->error('请选择单位');
        }
        $cate_id=$arr['cate_id'];
        $pidcategory = Db::name('category')->where('pid',$cate_id)->find();
        if($pidcategory){
            $this->error('请继续选择单位');
        }
        $category = Db::name('category')->where('id',$cate_id)->find();
        $arr['jobs_id']=$cate_id;
        $arr['jobs']=$category['name'];
        if($category['leveldata']=='3'){
            $arr['cate_id']=$category['pid'];
            $arr['catename']=Db::name('category')->where('id',$category['pid'])->value('name');
        }else{
            $arr['cate_id']=$cate_id;
            $arr['catename']=$category['name'];
        }

        $zongcate = Db::name('category')->field('id,name,pid')->select()->toArray();
        $companyid=gettop($zongcate,$cate_id);
        $arr['company']=Db::name('category')->where('id',$companyid)->value('name');

        $cid = Db::name('category')->where('id',$arr['jobs_id'])->value('switch');
        if($cid == '1'){
            $arr['pid'] = 0;//说明是部门负责人 , 可查看当前部门下的所有人员信息
            $arr['role'] = '1';
        }else{
            $pid = Db::name('auser')->where('cate_id',$arr['cate_id'])->where('role','1')->value('id');
            $arr['pid'] = $pid;
        }
        $ret = Db::name('auser')->where('id', $this->_uid)->update($arr);
        if ($ret){
            $this->success('修改信息成功');
        }
        $this->error('修改失败');
    }


    /*
     * 获取个人信息
     * */
    public function ainfo()
    {
        // 姓名 头像 部门  收藏数 课程数  考试数
        $user = Db::name('auser')->where('id', $this->_uid)->find();
        if (!$user)$this->error('查无此人信息');
        //查询此人是否为机场领导角色
        if ($user['cate_id'] == 1){
            $user['leader'] = '1';
        }else{
            $user['leader'] = '0';
        }
        $user['isleader']=Db::name('category')->where('id',$user['cate_id'])->value('isleaderswitch');
        if(empty($user['isleader'])){
            $user['isleader']='0';
        }
        $user['zans_count'] = Db::name('zans')->where('auser_id',$this->_uid)->count()??0;
        $user['exams_count'] = Db::name('exam_record')->where('auser_id',$this->_uid)->count()??0;
        //考试需要加上未测试的数
        $already = Db::name('exam_record')->where(['auser_id'=>$this->_uid])->column('exam_id');
        //未测试的考试
        $avideo_ids=cate_avideo($this->_uid);
        $undo = Db::name('exam')
            ->whereNotIn('id',$already)
            ->whereIn('avideo_id',$avideo_ids)
            ->count();
        $user['exams_count'] =  $user['exams_count'] + $undo;
        
        $user['colles_count'] = Db::name('colles')->where('auser_id',$this->_uid)->count()??0;
        $redis = Redis::getInstance()->getRedisConn();
        $re = $redis->hGetAll('user_'.$this->_uid)??[];
        $user['class_count'] = sizeof($avideo_ids);

        $jobs_id=$user['jobs_id'];
        $cates=pid_cate($jobs_id);
        $jobscate=Db::name('category')->where('id',$jobs_id)->find();

        $cid = Db::name('category')->where('id',$jobs_id)->value('switch');
        if($cid == '1'||$user['isleader']=='1'){
            $user['pid'] = 0;//说明是部门负责人 , 可查看当前部门下的所有人员信息
            $user['role'] = '1';
        }else{
            $user['role'] = '0';
        }
        $catearr=array();
        for ($i=count($cates)-1;$i>=0;$i--){
            $catearr['id']=$cates[$i];
            $cate=Db::name('category')->where('id',$cates[$i])->find();
            $catearr['name']=$cate['name'];
            if(($jobscate['leveldata']!='1')){
                if($cate['leveldata']!='1'){
                    $cats[]=$catearr;
                }
            }else{
                $cats[]=$catearr;
            }
        }
        $user['cates']=$cats;
        $this->success('成功',$user);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 组织架构图
     */
    public function zuzhi()
    {
        //查找其他部门
        $pid=gongsi($this->_uid);

        $cate = Db::name('category')->where('pid',$pid)->field('id,name')->select()->toArray();
        foreach ($cate as $k=>$v){
            if ($v['id'] == 49){
                unset($cate[$k]);
                continue;
            }
            $ids=pid_allcate($v['id']);
            $ids[]=$v['id'];
            $cate[$k]['user'] = Db::name('auser')->whereIn('cate_id',$ids)->field('id,name,avatar,jobs')->select();
            unset($ids);
        }
//        $arr = array_merge($tiyan,$cate);
        $this->success('成功',$cate);
    }
    /*
     * 查看是否已审核通过
     * */
    public function ischecked()
    {
        $uid = $this->_uid;
        $res = Db::name('auser')->where('id',$uid)->value('ischeck');
        $this->success('成功',['ischeck'=>$res]);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取我的视频列表,需要用户的ID
     */
    public function getrandom()
    {
        $page = $this->request->param('page')??1;
        $num = $this->request->param('num')??5;
        $where=null;
        $redis = Redis::getInstance()->getRedisConn();
        $re = $redis->hGetAll('user_'.$this->_uid);
        $yikanids=array_keys($re);//已看视频
        $avideo_ids=cate_avideo($this->_uid);//所有可看视频
        $vidnots=is_del_avideo($this->_uid);//已删除视频
        $vmodel = new \app\admin\model\Avideo();
        $data = $vmodel::with([
            'com' => function($query) {
                $query->order('id','desc');
            }
        ])->with('category')
            ->whereIn('avideo.id',$avideo_ids)
            ->whereNotIn('avideo.id',$vidnots)
            ->whereNotIn('avideo.id',$yikanids)
            ->page($page,$num)
            ->order('weigh asc')
            ->select()->each(function ($item){
                if ($item['com']){
                    //查询评论人信息
                    foreach ($item['com'] as $k => $v) {
                        $user = \app\admin\model\Auser::get($v['auser_id']);
                        if(empty($user)){
                            unset($item['com'][$k]);
                        }else{
                            $item['com'][$k]['name'] = $user['name'];
                            $item['com'][$k]['avatar'] = $user['avatar'];
                        }

                    }
                    $coms=$item['com'];
                    $commms=[];
                    foreach ($item['com'] as $k => $v){
                        if($item['com'][$k]['pid']>0){
                            unset($item['com'][$k]);
                        }else{
                            $arr=[];
                            foreach ($coms as $ve) {
                                if ($ve['pid'] == $item['com'][$k]['id']) {
                                    $arr[] = $ve;
                                };
                            };
                            $item['com'][$k]['children']=$arr;
                            $commms[]=$item['com'][$k];
                        }
                    }
                    $item['com']=$commms;
                    $item['comments'] = sizeof($commms);
                }else{
                    $item['comments'] = 0;
                }
                $item['article_title'] = Db::name('article')->where('avideo_id',$item['id'])->order('id desc')->value('title');
                $item['iscolles'] =Db::name('colles')->where('auser_id',$this->_uid)->where('avideo_id',$item['id'])->count();
                $item['iszan'] = Db::name('zans')->where('auser_id',$this->_uid)->where('avideo_id',$item['id'])->count();
                if(empty($item['article_title'])){
                    $item['article_title']='';
                }
                //判断当前请求的用户是否点赞该视频
                return $item;
            })->toArray();
        $this->success('成功',$data);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取单个视频的相关信息
     */
    public function singlev()
    {
        $vid = $this->request->param('vid');
        $vmodel = new \app\admin\model\Avideo();
        $data = $vmodel::with('com,category')
            ->where('avideo.id',$vid)
            ->order('ctime desc')
            ->find();
        if (!$data)$this->error('无数据');
        if (sizeof($data['com']) > 0){

            //查询评论人信息
            foreach ($data['com'] as $k => $v) {
                $user = \app\admin\model\Auser::get($v['auser_id']);
                if(empty($user)){
                    unset($data['com'][$k]);
                }else{
                    $data['com'][$k]['name'] = $user['name'];
                    $data['com'][$k]['avatar'] = $user['avatar'];
                }
            }
            $coms=$data['com'];
            $commms=[];
            foreach ($data['com'] as $k => $v){
                if($data['com'][$k]['pid']>0){
                    unset($data['com'][$k]);
                }else{
                    $arr=[];
                    foreach ($coms as $ve) {
                        if ($ve['pid'] == $data['com'][$k]['id']) {
                            $arr[] = $ve;
                        };
                    };
                    $item['com'][$k]['children']=$arr;
                    $commms[]=$item['com'][$k];
                }
            }
            $data['comments'] = sizeof($commms);
            $item['com']=$commms;
        }else{
            $data['comments'] = 0;
        }
        $data['article_title'] = Db::name('article')->where('avideo_id',$data['id'])->order('id desc')->value('title');
        $data['iscolles'] =Db::name('colles')->where('auser_id',$this->_uid)->where('avideo_id',$data['id'])->count();
        $data['iszan'] = Db::name('zans')->where('auser_id',$this->_uid)->where('avideo_id',$data['id'])->count();
        if(empty($data['article_title'])){
            $data['article_title']='';
        }
        $this->success('成功',$data);
    }
}